
//
//  MyCollectDemandTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/30.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyCollectDemandTabVC.h"
#import "DemandCell.h"
//#import "SubjectDemandModel.h"
#import "HomeModel.h"
@interface MyCollectDemandTabVC ()<DemandCellDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,assign) NSInteger id;
@property(nonatomic,assign) NSInteger uid;
@property(nonatomic,assign) NSInteger rid;
@property(nonatomic,assign) NSInteger did;
@property(nonatomic,assign) NSInteger aid;
@property(nonatomic,assign) NSInteger sid;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,assign) NSInteger pageNumber;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) MJRefreshAutoNormalFooter *footer;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@end

@implementation MyCollectDemandTabVC
static NSString *identifierNeed = @"DemandCell";
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
        
    }
    return _demandArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.manager = [AFHTTPSessionManager manager];
    [self.tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifierNeed];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectioncollectionDemand:) name:@"collectioncollectionDemand" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_header = header;
    [header beginRefreshing];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectDemandContent:) name:@"collectDemandContent" object:nil];
}
-(void)qingqiusibai:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    [self.tableView.mj_header endRefreshing];
    [WSProgressHUD dismiss];
}
-(void)refresh
{
    self.demandArray = nil;
    self.pageNumber = 1;
    self.pageSize = 10;
    self.uid = 1;
    self.id = 1;
    [self reloadInternetData];
}
-(void)collectDemandContent:(NSNotification *)info
{
    [self refresh];
}

-(void)reloadMore
{
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.pageNumber ++;
    [self reloadInternetData];
    
}

-(void)reloadInternetData
{
    NSString *pageNums = [NSString stringWithFormat:@"%ld",self.pageNumber];
    NSString *pageSizes = [NSString stringWithFormat:@"%ld",self.pageSize];
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"pageNumber":pageNums,
                  @"pageSize":pageSizes,
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:26 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)collectioncollectionDemand:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    NSArray *date = [dict objectForKey:@"date"];
    XLog(@"date:%@",date);
    if (date.count) {
        for (NSDictionary *dict in date) {
               HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
            [self.demandArray addObject:model];
        }
    }else {
       
    }
    if (self.demandArray.count) {
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
        self.tableView.mj_footer = self.footer;
        [self.footer setTitle:@"~~~ 我是有底线的 ~~~" forState:MJRefreshStateNoMoreData];
        self.footer.stateLabel.font = [UIFont systemFontOfSize:12];
        self.footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        if (date.count&&date.count< 10) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
    }else {
        [self.tableView.mj_footer removeFromSuperview];
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
    [WSProgressHUD dismiss];
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.demandArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    DemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierNeed forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HomeModel *model = self.demandArray[indexPath.row];
    cell.collectShow = NO;
    cell.delegate = self;
    cell.indexPath = indexPath;
    cell.homeModel = model;
    return cell;
   
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 144;
 
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HomeModel *model = self.demandArray[indexPath.row];
    if (self.returnValueBlock) {
        self.returnValueBlock(indexPath.row,model);
    };
}
#pragma mark-代理:收藏需求
- (void)demandCell:(DemandCell *)demandCell withCollectBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath withHomeModel:(HomeModel *)homeModel
{
    HomeModel *model = self.demandArray[indexPath.row];
    model.show = clickBtn.selected;
//    if (self.returnValueBlock) {
//        self.returnValueBlock(indexPath.row, model);
//        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//    [self refresh];
////    };
//    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark-代理:显示联系人
-(void)demandCell:(DemandCell *)demandCell withClickBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath withHomeModel:(HomeModel *)homeModel
{
    
    if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]) {
        [LogOutAccount presentOutAccount];
        [UIAlertViewWarningAccount showWarningAccount];
    }else if ([UserDefaultsHelper readAutoLogIn].length == 0) {
        [LogOutAccount presentOutAccount];
    }else {
        self.dict = @{
                      @"userId":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken]
                      };
        XLog(@"self.dict:%@",self.dict);
        self.urlString = [NSString stringWithFormat:@"%@/user/checkToken",kUrl];
        [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
            if (status == 200) {
                HomeModel *model = self.demandArray[indexPath.row];
                model.show = clickBtn.selected;
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }else {
                [UIAlertViewWarningAccount showWarningAccount];
                [LogOutAccount presentOutAccount];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }
}
@end
